//
//  JCWAccountTrilogyAlertView.swift
//  HengYouCai
//
//  Created by wll on 2017/12/5.
//  Copyright © 2017年 lwj. All rights reserved.
//  三步曲弹框--代码（实名认证、交易密码、风险测评）

import UIKit

class JCWAccountTrilogyAlertView: UIView {
    ///回调函数
    private var callBack: (()->Void)?
    ///头部图片名字
    private var headerImageNamed: String = "Details_inv_popup"
    ///MARK:构造函数
    
    ///三步曲弹框 传入头部图片名字(默认是Details_inv_popup)
    @discardableResult init(headerImageNamed: String?, callBack: (()->Void)?) {
        super.init(frame: UIScreen.main.bounds)
        
        self.headerImageNamed = headerImageNamed ?? "Details_inv_popup"
        self.callBack = callBack
        
        self.createUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    //MARK:添加UI
    private func createUI() {
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        UIApplication.shared.keyWindow?.addSubview(self)
        self.addSubview(self.cv)
        
        self.cv.addSubview(self.closeBtn)
        
        self.cv.addSubview(self.footerView)
        
        self.footerView.addSubview(self.headerImV)
        
        self.footerView.addSubview(self.midImV)
        
        self.footerView.addSubview(self.bottomBtn)
        
        self.footerView.layer.cornerRadius = 10
        self.footerView.clipsToBounds = true
        
        self.cv.layer.cornerRadius = 10
        self.cv.clipsToBounds = true
        
        self.footerView.addSubview(firstLb)
        
        self.footerView.addSubview(secondLb)
        
        self.footerView.addSubview(threeLb)
    }
    
    //MARK:布局
    override func layoutSubviews() {
        super.layoutSubviews()
        var cvH: CGFloat = 0
//        let cvW: CGFloat = self.headerImV.frame.size.width
        let cvW: CGFloat = 270
        
        self.closeBtn.frame = CGRect(x: cvW-self.closeBtn.frame.size.width, y: cvH, width: self.closeBtn.frame.size.width, height: self.closeBtn.frame.size.height)
        cvH += self.closeBtn.frame.size.height
        
        self.footerView.frame = CGRect(x: 0, y: cvH, width: cvW, height: 230)
        cvH += 230
        
        self.headerImV.frame = CGRect(x: -2.5, y: 0, width: cvW + 5, height: 90)
//        cvH += 90
        
//        self.midImV.frame = CGRect(x: 10, y: cvH - 5, width: cvW, height: 20)
//        cvH += 20
        
        
        self.midImV.frame = CGRect(x: 10, y: 90, width: cvW - 20, height: 40)
        
        self.firstLb.frame = CGRect(x: 10, y: 125, width: cvW / 3.0 - 10, height: 20)
        self.secondLb.frame = CGRect(x: cvW / 3.0, y: 125, width: cvW / 3.0, height: 20)
        self.threeLb.frame = CGRect(x: cvW - cvW / 3.0, y: 125, width: cvW / 3.0 - 10, height: 20)
        
        self.bottomBtn.frame = CGRect(x: 25, y: 165, width: cvW-50, height: 38)
        
        self.cv.bounds = CGRect(x: 0, y: 0, width: cvW, height: cvH)
        self.cv.center = CGPoint(x: self.center.x, y: self.center.y-self.closeBtn.frame.size.height)
    
    }
    
    //MARK:懒加载
    ///容器
    lazy var cv: UIView = {
        let temp = UIView()
        return temp
    }()
    
    ///右上角关闭按钮
    lazy var closeBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.frame = CGRect.zero
        temp.setImage(UIImage(named: "accountrilogalertview_close"), for: .normal)
        temp.sizeToFit()
        temp.addTarget(self, action: #selector(closeBtnClick), for: .touchUpInside)
        return temp
    }()
    
    ///头部图片
    lazy var headerImV: UIImageView = {
        let temp = UIImageView(frame: CGRect.zero)
        temp.image = UIImage(named: self.headerImageNamed)
//        temp.sizeToFit()
        temp.contentMode = .scaleAspectFill
        return temp
    }()
    
    ///中间图片
    lazy var midImV: UIImageView = {
        let temp = UIImageView(frame: CGRect.zero)
        temp.backgroundColor = UIColor.white
        temp.image = UIImage(named: "accav_midimage")
//        temp.sizeToFit()
        temp.contentMode = .scaleAspectFit
        return temp
    }()
    
    ///尾部容器
    lazy var footerView: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor.white
        return temp
    }()
    
    ///底部按钮
    lazy var bottomBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.setBackgroundImage(UIImage(named: "btn"), for: .normal)
        temp.setTitle("去完成", for: .normal)
        temp.titleLabel?.font = Font_WL(14)
        temp.addTarget(self, action: #selector(bottomBtnClick), for: .touchUpInside)
        return temp
    }()
    
    /// 第一步
    lazy var firstLb: UILabel = {
        let lablel = UILabel()
        lablel.textColor = Normal_333Color
        lablel.font = Font_WL(12)
        lablel.textAlignment = .left
        lablel.text = "实名认证"
        return lablel
    }()
    /// 第二步
    lazy var secondLb: UILabel = {
        let lablel = UILabel()
        lablel.textColor = Normal_333Color
        lablel.font = Font_WL(12)
        lablel.textAlignment = .center
        lablel.text = "设置交易密码"
        return lablel
    }()
    /// 第三步
    lazy var threeLb: UILabel = {
        let lablel = UILabel()
        lablel.textColor = Normal_333Color
        lablel.font = Font_WL(12)
        lablel.textAlignment = .right
        lablel.text = "风险测评"
        return lablel
    }()
    
    //MARK:按钮点击事件
    func closeBtnClick() {
        self.removeFromSuperview()
    }
    
    func bottomBtnClick() {
        self.callBack?()
        self.removeFromSuperview()
    }
    
}
